Java remote method invocation
part 5/7 · 10.4 KB total
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
//Instantiate RmiServer
RmiServer server = new RmiServer();
// Bind this object instance to the name "RmiServer"
Naming.rebind("//localhost/RmiServer", server);
System.out.println("PeerServer bound in registry");
}
}
RmiClient class
this is the client which gets the reference (a proxy) to the remote object living on the server and invokes its method to get a message. If the server object implemented java.io.Serializable instead of java.rmi.Remote, it would be serialized and passed to the client as a value.cite-ref-2[2]
import java.rmi.Naming;
public class RmiClient {
public static void main(String[] args) throws Exception {
RmiServerIntf server = (RmiServerIntf)Naming.lookup("//localhost/RmiServer");
System.out.println(server.getMessage());
}
}
Before running this example, we need to make a 'stub' file for the interface we used. For this task we have the RMI compiler - 'rmic'
• Note: we make a stub file from the '*.class' file with the implementation of the remote interface, not from the '*.java' file.
rmic RmiServer
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────